﻿using Common.Mvvm.Navigation.Regions;
using Syncfusion.Windows.Tools.Controls;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Syncfusion.Lib.Extensions
{

    public class SfDockingManagerRegionAdapter : RegionAdapterBase<DockingManager>
    {
        public SfDockingManagerRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory)
            : base(regionBehaviorFactory)
        {

        }

        protected override void Adapt(IRegion region, DockingManager regionTarget)
        {
            region.Views.CollectionChanged += (s, e) =>
            {
                if (e.Action == NotifyCollectionChangedAction.Move)
                {
                    // Todo：判定是否是拖拽的内容为首页
                }
                if (e.Action == NotifyCollectionChangedAction.Add)
                {
                    if (e.NewItems != null)
                    {
                        foreach (FrameworkElement element in e.NewItems)
                        {
                            if (!regionTarget.Children.Contains(element))
                            {
                                regionTarget.BeginInit();
                                regionTarget.Children.Add(element);
                                regionTarget.EndInit();
                            }
                        }
                    }
                }
            };
        }

        protected override void AttachBehaviors(IRegion region, DockingManager regionTarget)
        {
            base.AttachBehaviors(region, regionTarget);
            if (!region.Behaviors.ContainsKey(SfDocumentRegionActiveAwareBehavior.BehaviorKey))
                region.Behaviors.Add(SfDocumentRegionActiveAwareBehavior.BehaviorKey, new SfDocumentRegionActiveAwareBehavior { HostControl = regionTarget });
        }

        protected override IRegion CreateRegion()
        {
            return new SingleActiveRegion();
        }
    }
}
